//
//  SearchResultViewController.m
//  FingerUniversity
//
//  Created by vantle on 16/3/16.
//  Copyright © 2016年 vantle. All rights reserved.
//

#import "SearchResultViewController.h"
#import "AFNetworking.h"
#import "MTLJSONAdapter.h"
#import "NoticeModel.h"
#import "NoticeCell.h"
#import "MobClick.h"
#import "File.h"
#import "ClassCell.h"
#import "NoticeDetailViewController.h"
#import "SubClassViewController.h"
#import "ClassDetailViewController.h"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
@interface SearchResultViewController ()

@end
@implementation SearchResultViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"SearchResultViewController"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"SearchResultViewController"];
}
-(void)viewDidLoad
{
    self.view.backgroundColor=[UIColor whiteColor];
    UIImageView*bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    bg.image=[UIImage imageNamed:@"bg"];
    [self.view addSubview:bg];
    
    UIImageView*barImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 44)];
    barImage.userInteractionEnabled=YES;
    barImage.backgroundColor=[UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
    [self.view addSubview:barImage];
    
    UIButton*backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(0, 0, 100, 44);
    [backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [barImage addSubview:backButton];
    
    UIImageView*backImage=[[UIImageView alloc]initWithFrame:CGRectMake(5, 12, 5, 20)];
    backImage.image=[UIImage imageNamed:@"back_arrowleft"];
    [barImage addSubview:backImage];
    
    UIImageView*logoImage=[[UIImageView alloc]initWithFrame:CGRectMake(backImage.frame.origin.x+backImage.frame.size.width+3, 6, 32, 32)];
    logoImage.layer.cornerRadius=3;
    logoImage.clipsToBounds=YES;
    logoImage.image=[UIImage imageNamed:@"app-logo-80"];
    [barImage addSubview:logoImage];
    
    UILabel*noticeLabel=[[UILabel alloc]initWithFrame:CGRectMake(logoImage.frame.origin.x+logoImage.frame.size.width+5, 10, 150, 25)];
    noticeLabel.text=@"搜索结果";
    [barImage addSubview:noticeLabel];
    
    _listArray=[[NSMutableArray alloc]init];
    
//    if (self.viewTag==2012) {
    
        [self getNoticeInfo];
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.separatorColor=[UIColor whiteColor];
        _tableView.backgroundColor=[UIColor clearColor];
        [self.view addSubview:_tableView];
//    }
//    else
//    {
//        [self getClassInfo];
//        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
//        _tableView.dataSource=self;
//        _tableView.delegate=self;
//        _tableView.separatorColor=[UIColor whiteColor];
//        _tableView.backgroundColor=[UIColor clearColor];
//        [self.view addSubview:_tableView];
//    }
    
}
//-(void)getClassInfo
//{
//    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
//    NSString*userId=[userDefaults objectForKey:@"userId"];
//    NSString*flag=[userDefaults objectForKey:@"flag"];
//    
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    
//    //    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    //    manager.responseSerializer.acceptableContentTypes =[manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
//    
//    NSDictionary*params=@{@"userId":userId,@"flag":flag,@"start":@"0",@"end":[NSString stringWithFormat:@"%d",10],@"name":self.result,@"classify":@"0",@"speaker":@""};
//    //    manager.responseSerializer=[AFJSONResponseSerializer serializer];
//    
//    [manager POST:@"http://120.76.79.85:9080/mzdx/service/courseList.action" parameters:params success: ^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        NSData*responseData=responseObject;
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData
//                                                            options:NSJSONReadingMutableContainers
//                                                              error:nil];
//        NSString*rType=[dic objectForKey:@"rType"];
//        if ([rType isEqualToString:@"1"]) {
//            [_listArray removeAllObjects];
//            NSArray*coursesArray=[dic objectForKey:@"courses"];
//            NSLog(@"=======%@",coursesArray);
//            if ([coursesArray isKindOfClass:[NSArray class]]) {
//                for (NSDictionary*tempDic in coursesArray) {
//                    NSError *error = nil;
//                    File*fileModel=[[File alloc]init];
//                    fileModel=[MTLJSONAdapter modelOfClass:[File class] fromJSONDictionary:tempDic error:&error];
//                    if (error==nil) {
//                        [_listArray addObject:fileModel];
//                        
//                    }
//                    
//                }
//                
//            }
//            
//            [_tableView reloadData];
//            _tableView.frame=CGRectMake(0, 64, ScreenWidth, _listArray.count*60);
//            
//        }
//        
//    } failure: ^(AFHTTPRequestOperation *operation,NSError*error) {
//        
//        NSLog(@"%@",error);
//    }];
//}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (self.viewTag==2012) {
        static NSString*cellIndentifier=@"cell";
        NoticeCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell==nil) {
            cell=[[NoticeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.titleLabel.text=[_listArray[indexPath.row] title];
        cell.timeLbel.text=[_listArray[indexPath.row] createTime];
        cell.backgroundColor=[UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:0.5];
        NSString*classify=[_listArray[indexPath.row] classify];
        if ([classify isEqualToString:@"1"]) {
            cell.myImage.image=[UIImage imageNamed:@"通知"];
        }
        else if ([classify isEqualToString:@"2"]) {
            cell.myImage.image=[UIImage imageNamed:@"校发文件"];
        }
        else if ([classify isEqualToString:@"3"]) {
            cell.myImage.image=[UIImage imageNamed:@"-校办文件"];
        }
        else
        {
            cell.myImage.image=[UIImage imageNamed:@"工部文件"];
        }
        
        return cell;
//    }
//    else
//    {
//        static NSString*cellIndentifier=@"celll";
//        ClassCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIndentifier];
//        if (cell==nil) {
//            cell=[[ClassCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
//        }
//        cell.titleLabel.text=[_listArray[indexPath.row] name];
//        cell.backgroundColor=[UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:0.5];
//        cell.numberLabel.text=[NSString stringWithFormat:@"%d",(int)indexPath.row+1];
//        cell.selectionStyle=UITableViewCellSelectionStyleNone;
//        return cell;
//    }
    
}

-(void)getNoticeInfo
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString*userId=[userDefaults objectForKey:@"userId"];
    NSString*flag=[userDefaults objectForKey:@"flag"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes =[manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
    NSDictionary*params=@{@"userId":userId,@"flag":flag,@"start":@"0",@"end":@"20",@"title":self.result,@"classify":@""};
    
    [manager POST:@"http://120.76.79.85:9080/mzdx/service/noticeList.action" parameters:params success: ^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSData*responseData=responseObject;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData
                                                            options:NSJSONReadingMutableContainers
                                                              error:nil];
        NSLog(@"------%@",dic);
        NSString*rType=[dic objectForKey:@"rType"];
        if ([rType isEqualToString:@"1"]) {
            [_listArray removeAllObjects];
            
            NSArray*noticeArray=[dic objectForKey:@"notices"];
            
            if ([noticeArray isKindOfClass:[NSArray class]]) {
                
                for (NSDictionary*tempDic in noticeArray) {
                    NSError *error = nil;
                    NoticeModel*noticeModel=[[NoticeModel alloc]init];
                    noticeModel=[MTLJSONAdapter modelOfClass:[NoticeModel class] fromJSONDictionary:tempDic error:&error];
                    if (error==nil) {
                        [_listArray addObject:noticeModel];
                        
                        
                    }
                    
                }
                
            }
            
        }
        [_tableView reloadData];
        if ((ScreenHeight-64)/60>_listArray.count) {
            
            _tableView.frame=CGRectMake(0, 64, ScreenWidth, _listArray.count*60);
        }
        else
        {
            _tableView.frame=CGRectMake(0, 64, ScreenWidth, ScreenHeight-64);
        }
        
        
        
    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (self.viewTag==2012) {
        NoticeDetailViewController*noticeDetailVC=[[NoticeDetailViewController alloc]init];
        NoticeModel*model=_listArray[indexPath.row];
        self.noticeIdChuan=model.noticeId;
        noticeDetailVC.noticeIdQu=self.noticeIdChuan;
        [self.navigationController pushViewController:noticeDetailVC animated:YES];
//    }
//    else
//    {
//        File*fileModel=_listArray[indexPath.row];
//        int isDir=fileModel.isDir;
//        
//        if (isDir==1) {
//            SubClassViewController*subClassVC=[[SubClassViewController alloc]init];
//            self.courseIdCun=fileModel.courseId;
//            subClassVC.courseIdQu=self.courseIdCun;
//            [self.navigationController pushViewController:subClassVC animated:YES];
//        }
//        else
//        {
//            ClassDetailViewController*classDetailVC=[[ClassDetailViewController alloc]init];
//            self.courseIdCun=fileModel.courseId;
//            classDetailVC.QuId=self.courseIdCun;
//            [self.navigationController pushViewController:classDetailVC animated:YES];
//        }
//    }
}
-(void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewDidLayoutSubviews
{
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
@end
